local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "authsvrhelper"
local base = require "base"
local msgproxy = require "msgproxy"
local configdao = require "configdao"
local playerauthdao = require "playerauthdao"
local filename = "authsvrcmd.lua"
local AuthsvrCMD = {}

function AuthsvrCMD.process(session, source, event, ...)
	local f = AuthsvrCMD[event] 
	if f == nil then
		filelog.sys_error(filename.."Authsvrd AuthsvrCMD.process invalid event:"..event)
		return nil
	end
	f(...)
end

function AuthsvrCMD.start(conf)
	local server = msghelper:get_server()
	server.tcpmng.init(server, "agent", conf.agentsize, conf.agentincr, conf.svr_netpack)
	base.skynet_retpack(true)

	msgproxy.init(skynet.getenv("svr_id"))

	local status
	skynet.fork(function()
		skynet.sleep(1000)

		status, server.max_uid = playerauthdao.get_maxplayer_uid(skynet.getenv("svr_id"))

		if not status then
			filelog.sys_error("AuthsvrCMD.start failed: get_maxplayer_uid failed")
			return
		end

		skynet.call(server.gate_service, "lua", "open" , conf)
	end)
end

function AuthsvrCMD.close(...)
	local server = msghelper:get_server()
	server:exit_service()	
end

function AuthsvrCMD.agentexit(fd)
	local server = msghelper:get_server()
	server.tcpmng.agentexit(fd)
end

function AuthsvrCMD.reload(...)
	base.skynet_retpack(1)
	filelog.sys_error("AuthsvrCMD.reload start")

	configdao.reload()

	skynet.sleep(200)

	msgproxy.reload()
	
	filelog.sys_error("AuthsvrCMD.reload end")
end

return AuthsvrCMD